package com.liaowei.user.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liaowei.common.BaseResponse;
import com.liaowei.common.Constants;
import com.liaowei.common.ErrorCode;
import com.liaowei.exception.CustomerException;
import com.liaowei.model.domain.Tag;
import com.liaowei.model.domain.UserTag;
import com.liaowei.model.param.UserTagParam;
import com.liaowei.user.mapper.UserTagMapper;
import com.liaowei.user.openfeign.TagClient;
import com.liaowei.user.service.UserTagService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
* @author LindaMan
* @description 针对表【wy_user_tag(用户-标签表)】的数据库操作Service实现
* @createDate 2023-05-01 17:19:40
*/
@Service
@RequiredArgsConstructor
public class UserTagServiceImpl extends ServiceImpl<UserTagMapper, UserTag>
    implements UserTagService{

    private final UserTagMapper userTagMapper;
    private final TagClient tagClient;


    /**
     * 保存自己的兴趣标签
     * @param userTagParam 兴趣标签参数
     * @return 提示信息
     */
    @Transactional
    @Override
    public String saveInterestTagList(UserTagParam userTagParam) {
        // 检验是否是当前用户操作
        long loginId = StpUtil.getLoginIdAsLong();
        if (loginId != userTagParam.getUserId()){
            throw new CustomerException(Constants.PARAM_ERROR,"请登录合法用户");
        }

        // 判断是否要保存兴趣标签
        List<Long> tagIdList = userTagParam.getTagIdList();
        try {
            if (CollectionUtil.isEmpty(tagIdList)){
                // 删除所有兴趣标签
                userTagMapper.deleteUserTagByUserId(loginId);
            }else {
                // 保存兴趣标签
                // 判断兴趣标签是否合法
                BaseResponse<Integer> response = tagClient.countTagByIds(tagIdList);
                Integer count = response.getData();
                if (count != tagIdList.size()) {
                    throw new CustomerException(Constants.PARAM_ERROR, "无效标签");
                }
                // 先删除原有标签
                userTagMapper.deleteUserTagByUserId(loginId);
                // 保存新的标签
                List<UserTag> userTagList = tagIdList.stream().map(id -> {
                    UserTag userTag = new UserTag();
                    userTag.setUserId(loginId);
                    userTag.setTagId(id);
                    return userTag;
                }).collect(Collectors.toList());
                this.saveBatch(userTagList);
            }
        } catch (CustomerException e) {
            throw new CustomerException(ErrorCode.SYSTEM_ERROR);
        }

        return "保存成功";
    }

    /**
     * 查询自己的兴趣标签
     * @param userId 用户id
     * @return List<Tag>
     */
    @Override
    public List<Tag> searchInterestTagList(long userId) {
        List<Tag> tagList = userTagMapper.selectInterestTagList(userId);
        return Optional.ofNullable(tagList).orElse(new ArrayList<>());
    }

    /**
     * 根据标签删除用户标签记录
     * @param tagId 标签id
     * @return 提示信息
     */
    @Transactional
    @Override
    public String deleteByTagId(long tagId) {
        try {
            userTagMapper.delete(Wrappers.lambdaQuery(UserTag.class).eq(UserTag::getTagId,tagId));
        } catch (Exception e) {
            throw new CustomerException(ErrorCode.SYSTEM_ERROR);
        }

        return "删除成功";
    }
}




